package com.a918.service.impl;

import com.a918.dao.ScoreMapper;
import com.a918.entity.Score;
import com.a918.service.EvaluateService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class EvaluateServiceImp implements EvaluateService {
    private ScoreMapper scoreMapper;

    @Autowired
    public EvaluateServiceImp(ScoreMapper scoreMapper){
        this.scoreMapper=scoreMapper;
    }

    @Override
    public Score getScoreByNameAndYuefen(String wangdian,String yuefen){
        return scoreMapper.getByNameAndYuefen(wangdian,yuefen);
    }

    @Override
    public Map<String,Object> getMinAndMaxZhibiao(String yuefen) {
        Map<String,Object> result=new HashMap();
        Map<String,Map<String,Float>> twoMaps=new HashMap();
        List<String> list= Arrays.asList("Xingfudu","Services","Shixiao","Peisongsd","Chulisd","Shengchanlv","Zhouzhuanlv");
        Map<String,Float> maxMap=new HashMap<>();
        Map<String,Float> minMap=new HashMap<>();
        try {
            for(String zb:list){
                if(scoreMapper.getMinOrMaxZhibiao(zb,yuefen,0).size()==0){
                    result.put("res","fail");
                    return new JSONObject(result);
                };
                Score score=scoreMapper.getMinOrMaxZhibiao(zb,yuefen,0).get(0);
                Method m=score.getClass().getDeclaredMethod("get"+zb);
                minMap.put(zb,(Float)m.invoke(score));
                score=scoreMapper.getMinOrMaxZhibiao(zb,yuefen,1).get(0);
                maxMap.put(zb,(Float)m.invoke(score));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        twoMaps.put("min",minMap);
        twoMaps.put("max",maxMap);
        result.put("res",twoMaps);
        return result;
    }

    @Override
    public Map<String,Object> getShengchanlvByNameAndYueRange(String wangdian,String yuefenBgn,String yuefenEnd){
        List<Score> scoreList=scoreMapper.getByNameAndYueRange(wangdian,yuefenBgn,yuefenEnd);
        Map<String,Object> res=new HashMap<>();
        if(!scoreList.isEmpty()) {
            List<String> yuefenList=new ArrayList<>();
            List<Float>  sclList=new ArrayList<>();
            for (Score score : scoreList) {
                yuefenList.add(score.getYuefen());
                sclList.add(score.getShengchanlv());
            }
            res.put("yuefen",yuefenList);
            res.put("shengchanlv",sclList);
            res.put("res","sccuss");
        }else{
            res.put("res","fail");
        }
        return res;
    }
}
